/**
 * A component for rendering ranking information about all cities
 */
Ext.define('okcapital.widgets.CityRankingPanel', {
    extend: 'Ext.grid.Panel',
    
    alias : 'widget.cityrankingpanel',

    /** Instance of Ext.data.Store of okcapital.model.City instances*/
    cityStore : null,
    
    currentRequests : null,
    
    rankingStore : null,
    

    constructor : function(cfg) {
        this.cityStore = cfg.cityStore;
        this.currentRequests = [];
        this.rankingStore = Ext.create('Ext.data.Store', {
        	model : 'okcapital.model.CityRanking'
        });
        
        this.initRankingStore();
        this.cityStore.on('load', this.initRankingStore, this);
        
        Ext.apply(cfg, {
        	store : this.rankingStore,
        	columns : [{
        		text : 'City',
        		dataIndex : 'name'
        	},{
        		text : 'Score',
        		dataIndex : 'rating'
        	}]
        });
        
        this.callParent(arguments);
    },
    
    initRankingStore : function() {
    	for (var i = 0; i < this.cityStore.getCount(); i++) {
        	var city = this.cityStore.getAt(i);
        	this.rankingStore.add(Ext.create('okcapital.model.CityRanking', {
        		cityId : city.get('cityId'),
        		name : city.get('name'),
        		rating : 1.0
        	}));
    	}
    	
    	this.updateRankings();
    },
    
    /**
     * Update rankings from a list of dataset profiles
     */
    updateRankings : function(datasetProfiles) {
    	for (var i = 0; i < this.currentRequests.length ;i++) {
    		Ext.Ajax.abort(this.currentRequests[i]);
    	}
    	
    	var datasetIds = [];
    	var datasetWeightings = [];
    	if (datasetProfiles) {
	    	for (var i = 0; i < datasetProfiles.length; i++) {
	    		var profile = datasetProfiles[i];
	    		datasetIds.push(profile.get('datasetId'));
	    		datasetWeightings.push(profile.get('weighting'));
	    	}
    	}
    	
    	//Fire off requests
    	currentRequests = [];
    	for (var i = 0; i < this.rankingStore.getCount(); i++) {
        	var ranking = this.rankingStore.getAt(i);
        	var params = {
        		cityId : ranking.get('cityId'),
        		datasetId : datasetIds,
        		datasetWeighting : datasetWeightings
        	}
        	
        	this.currentRequests.push(Ext.Ajax.request({
        		url : 'getCityRatingNormalised.do',
        		params : params,
        		ranking : ranking,
        		rankingStore : this.rankingStore,
        		callback : function(options, success, response) {
        			if (!success) {
        				return;
        			}
        			
        			var jsonObj = Ext.JSON.decode(response.responseText);
        			if (!jsonObj || !jsonObj.success) {
        				return;
        			}
        			
        			var totalRating = jsonObj.data[0].rating;
        			
        			options.ranking.set('rating', totalRating);
        			options.rankingStore.sort({
        				property : 'rating',
        				direction : 'DESC'
        			})
        		}
        	}));
    	}
    }
});